﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PersonalAlbum.Data.Objects;

namespace PersonalAlbum.Data.Entities
{
    /// <summary>
    /// Manages the replies for the entries. This is done separately from the entries 
    ///  repository as the replies are added and edited on different times and places and 
    ///  not when the entry is created/edited
    /// </summary>
    public class ReplyRepository : IRepository<Reply>
    {
        private string _connectionString;
        public ReplyRepository(string connectionString)
        {
            this._connectionString = connectionString;
        }
        #region IRepository<Entry> Members

        public List<Reply> GetItems()
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                return ImagesDB.Reply.Select(r =>
                    new Reply()
                    {
                        Body = r.Body,
                        Email = r.Email,
                        EntryId = r.EntryId,
                        Id = r.Id,
                        IsApproved = r.Approved,
                        Subject = r.Subject,
                        TimeStamp = r.Timestamp,
                        UserName = r.Username
                    }).ToList();
            }
        }

        public void AddItem(Reply item)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                ImagesDal.Reply r = new ImagesDal.Reply()
                {
                    Username = item.UserName,
                    Timestamp = item.TimeStamp,
                    Subject = item.Subject,
                    Approved = item.IsApproved,
                    Body = item.Body,
                    EntryId = item.EntryId,
                    Email = item.Email
                };
                ImagesDB.AddToReply(r);
                ImagesDB.SaveChanges();
                item.Id = r.Id;
            }
        }

        public void DeleteItem(Reply item)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                var old = ImagesDB.Reply.FirstOrDefault(r => r.Id == item.Id);
                if (old != null)
                {
                    ImagesDB.DeleteObject(old);
                    ImagesDB.SaveChanges();
                }
            }
        }

        public void UpdateItem(Reply item)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                var old = ImagesDB.Reply.FirstOrDefault(r => r.Id == item.Id);
                if (old != null)
                {
                    old.Username = item.UserName;
                    old.Subject = item.Subject;
                    old.Approved = item.IsApproved;
                    old.Body = item.Body;
                    old.EntryId = item.EntryId;
                    old.Email = item.Email;
                    ImagesDB.SaveChanges();
                }
            }
        }


        public PersonalAlbum.Data.Objects.Reply GetItemById(int id)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                return ImagesDB.Reply.Where(r => r.Id == id).Select(r =>
                    new PersonalAlbum.Data.Objects.Reply()
                    {
                        Body = r.Body,
                        Email = r.Email,
                        EntryId = r.EntryId,
                        Id = r.Id,
                        IsApproved = r.Approved,
                        Subject = r.Subject,
                        TimeStamp = r.Timestamp,
                        UserName = r.Username
                    }).SingleOrDefault();
            }
        }

        #endregion
    }
}
